using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
using System.Linq;

namespace IQIGame.Onigao.GamePlay
{
    public class UIGmHistory : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIGmHistory
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ExLoopVerticalScrollRect scroll { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("scroll", out var __tbv0);
                this.scroll = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv1);
                this.btnClose = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        //PlayerPreferenceString historyLog;
        private LoopScrollAdapter<UIN_UIGmHistoryCell> gmHistoryCells = null;
        private List<string> GMList { get; set; } = new();
        #endregion fields

        #region properties

        protected UIB_UIGmHistory ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIGmHistory();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

            this.gmHistoryCells = this.InitLoopScroll<UIN_UIGmHistoryCell>(this.ui.scroll, this.OnHistoryChanged);
        }



        protected override void OnShow()
        {
            var historyLog = new PlayerPreferenceString(PlayerPrefsConst.GMHistory, string.Empty);
            GMList = historyLog.Value.Split('\n').Where(s => !string.IsNullOrEmpty(s)).ToList();
            gmHistoryCells.RefillCells(GMList.Count, 0);
        }

        protected override void OnHide()
        {

        }


        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnClose.AddClickEvent(OnClickClose);
        }

        private void OnClickClose()
        {
            this.Hide();
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            GMList.Clear();
        }

        #region methonds

        private void OnHistoryChanged(UIN_UIGmHistoryCell cell, int arg2)
        {
            var svData = this.GMList[arg2];
            cell.Show(svData);
        }

        #endregion methonds
    }
}